package com.wissen.temperatureapp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
//import com.google.gwt.user.client.ui.VerticalPanel;
import com.wissen.temperatureapp.client.constants.HistoryConstants;
import com.wissen.temperatureapp.client.controller.TempController;
import com.wissen.temperatureapp.client.observers.TemperatureObserver;
import com.wissen.temperatureapp.client.widgets.HomeWidget;
import com.wissen.temperatureapp.client.widgets.CelsiusWidget;
import com.wissen.temperatureapp.client.widgets.FahrenheitWidget;
import com.wissen.temperatureapp.client.widgets.ProgressBarWidget;
import com.wissen.temperatureapp.client.widgets.SliderBarWidget;

/**
 * Class represents Entry points. Entry point classes define <code>onModuleLoad()</code>.
 * 
 * @author Mayur Birari
 */

public class TemperatureApp implements EntryPoint, TemperatureObserver, ValueChangeHandler<String> {

    private HorizontalPanel containerPanel;

    private FlexTable temperatureTable;
    boolean Fahrenheitflag=false;
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
       
    	containerPanel = new HorizontalPanel();
    	temperatureTable=new FlexTable();
        RootPanel.get("pageContent").add(containerPanel);
        containerPanel.add(temperatureTable);
        temperatureTable.setWidget(0, 0,new CelsiusWidget());
        
        History.addValueChangeHandler(this);
        //very very important
        TempController.getInstance().addTempObserver(this);
    }

    /*public void displayCelsiusWidget() {
        containerPanel.clear();
        containerPanel.add(new CelsiusWidget());
    }*/

    public void displayAllWidget(Double value) {
      //  containerPanel.clear();
    	if(!Fahrenheitflag){
    		temperatureTable.setWidget(0,1,new FahrenheitWidget(value));
    		temperatureTable.setWidget(1,0,new ProgressBarWidget(value));
    		temperatureTable.setWidget(2,0,new SliderBarWidget(value));
        Fahrenheitflag=true;
    	}
    }
    public void displayHomeWidget() {
        containerPanel.clear();
        containerPanel.add(new HomeWidget());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.wissen.tempapp.client.observers.LoginObserver#notifyConvertFailed(
     * java.lang.String)
     */
    public void notifyConvertFailed(String errorMessage) {
        Window.alert("Conversion fail: " + errorMessage);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.wissen.tempapp.client.observers.TempObserver#notifyLoginSucceeded()
     */
    public void notifyConvertSucceeded(Double fahrenheitvalue) {
    	Window.alert("Conversion Succeed");
        displayAllWidget(fahrenheitvalue);
        //History.newItem(HistoryConstants.HOME_PAGE_HISTORY_TOKEN);
    }

    @Override
    public void onValueChange(ValueChangeEvent<String> event) {
        if (event.getValue().equals(HistoryConstants.HOME_PAGE_HISTORY_TOKEN)) {
            displayHomeWidget();
        }
    }
}
